package com.zl.easybuy.web;

import com.zl.easybuy.pojo.User;
import com.zl.easybuy.service.UserService;
import com.zl.easybuy.util.Constant;
import com.zl.easybuy.util.PasswordUtils;
import com.zl.easybuy.util.SessionUtil;
import com.zl.easybuy.vo.ReaultVo;
import com.zl.easybuy.vo.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RequestMapping("/User")
@Controller
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/Login")//登录判断
    @ResponseBody
    public ReaultVo<User> login(String loginName,String password){
        if(userService.login(loginName,password)){
                return ReaultVo.success(ResultCode.SUCCESS);
        }
        return ReaultVo.fail(ResultCode.LOGIN_FAIL);
    }
    @PostMapping("/Register")
    @ResponseBody
    public ReaultVo<User> register(User user){
        user.setType(Constant.type);
        if(userService.register(user)){
            return new ReaultVo<>(ResultCode.REGISTER_SUCCESS);
        }
        return new ReaultVo<>(ResultCode.REGISTER_FAIL);
    }
    @RequestMapping("/page/{pageName}")//restful风格页面跳转
    public String toPage(@PathVariable String pageName){
        return "redirect:/pre/"+pageName+".jsp";
    }
    @RequestMapping("/LoginOut")
    public String loginOut(){
        SessionUtil.invalidUserSession();
        return "redirect:/pre/login.jsp";
    }
}
